import { ApiResponse } from "apisauce"

import { apiSauce } from "@/services/api"

const USER_PREFIX = "/gpt404/user"

export interface BaseResp {
  code: number
}

export interface VisitorLoginReq {
  uuid?: string
}

export interface LoginData {
  uuid: string
  accessToken: string
  refreshToken: string
}

export interface VisitorLoginResp extends BaseResp {
  data: LoginData
}

export interface SendSmsCodeReq {
  mobileCountryCode: string
  mobile: string
}

export interface SendSmsCodeData {
  phoneBindState: "notBind" | "selfBind" | "otherBind"
}

export interface SendSmsCodeResp extends BaseResp {
  data: SendSmsCodeData
}

export interface SmsLoginReq {
  mobileCountryCode: string
  mobile: string
  verificationCode: string
}

export interface SmsLoginResp extends BaseResp {
  data: LoginData
}

export interface RefreshTokenReq {
  refreshToken: string
}

export interface RefreshTokenData {
  accessToken: string
}

export interface RefreshTokenResp extends BaseResp {
  data: RefreshTokenData
}

export interface Credentials {
  tmpSecretId: string
  tmpSecretKey: string
  sessionToken: string
}

export interface CredentialResult {
  credentials: Credentials
  startTime: number
  expiredTime: number
  expiration: string
}

export interface GetCosUploadCredentialData {
  credentialResult: CredentialResult
  resourcePath: string
  bucket: string
  region: string
}

export interface GetCosUploadCredentialResp extends BaseResp {
  data: GetCosUploadCredentialData
}

export interface LoginMethod {
  provider: string
}

export interface GetUserData {
  mobileCountryCode: string | null
  mobile: string | null
  now: number
  registerDate: string
  registerAt: number
  loginMethods: LoginMethod[]
  userNum: string
}

export interface GetUserResp extends BaseResp {
  data: GetUserData
}

export interface SaveUserReq {
  apnsPushToken?: string
  name?: string
  avatar?: string
}

export interface GetConfigsReq {
  app?: string
}

export interface Config {
  key: string
  val: string
}

export interface GetConfigsData {
  configs: Config[]
}

export interface GetConfigsResp extends BaseResp {
  data: GetConfigsData
}

export const visitorLogin = (payload?: VisitorLoginReq): Promise<ApiResponse<VisitorLoginResp>> => {
  return apiSauce.post(`${USER_PREFIX}/visitor/login`, payload ?? {})
}

export const sendSmsCode = (payload: SendSmsCodeReq): Promise<ApiResponse<SendSmsCodeResp>> => {
  return apiSauce.post(`${USER_PREFIX}/send_sms_code`, payload)
}

export const smsLogin = (payload: SmsLoginReq): Promise<ApiResponse<SmsLoginResp>> => {
  return apiSauce.post(`${USER_PREFIX}/sms_login`, payload)
}

export const refreshToken = (payload: RefreshTokenReq): Promise<ApiResponse<RefreshTokenResp>> => {
  return apiSauce.post(`${USER_PREFIX}/refresh_token`, payload)
}

export const getCosUploadCredential = (): Promise<ApiResponse<GetCosUploadCredentialResp>> => {
  return apiSauce.get(`${USER_PREFIX}/cos_upload_credential`)
}

export const getUser = (): Promise<ApiResponse<GetUserResp>> => {
  return apiSauce.get(`${USER_PREFIX}/user`)
}

export const saveUser = (payload: SaveUserReq): Promise<ApiResponse<BaseResp>> => {
  return apiSauce.post(`${USER_PREFIX}/user`, payload)
}

export const getConfigs = (params?: GetConfigsReq): Promise<ApiResponse<GetConfigsResp>> => {
  return apiSauce.get(`${USER_PREFIX}/configs`, params)
}
